//
//  ZSBWordOrdorSearchConditionView.m
//  HCNewGoldFinger
//
//  Created by 刘琛 on 2017/12/13.
//  Copyright © 2017年 zjhcsoftios. All rights reserved.
//

#import "ZSBWordOrdorSearchConditionView.h"
#import "WordOrdorSearchConditionCell.h"


#define iSEESCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define iSEESCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface ZSBWordOrdorSearchConditionView () <UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource>

@property (copy, nonatomic) OrdorSearchFailureBlock fail;
@property (copy, nonatomic) OrdorSearchSuccessblock success;
@property (weak, nonatomic) IBOutlet UIView *collectionBackView;
@property (weak, nonatomic) IBOutlet UIView *backView;

@end

static NSString *wordOrdorSearchConditionCellIdentify = @"wordOrdorSearchConditionCellIdentify";


@implementation ZSBWordOrdorSearchConditionView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setCollectionViewAndOtherViews];
    }
    return self;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setCollectionViewAndOtherViews];
}

- (void)setCollectionViewAndOtherViews {
    
    _backView.layer.cornerRadius = 5;
    _backView.layer.masksToBounds = YES;
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(5, 10, 10, 10);
    
    _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, iSEESCREEN_WIDTH - 60, 220) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    
    [_collection registerNib:[UINib nibWithNibName:@"WordOrdorSearchConditionCell" bundle:[NSBundle bundleWithURL:[[NSBundle bundleForClass:[self class]] URLForResource:@"NewMarketLib" withExtension:@"bundle"]]] forCellWithReuseIdentifier:wordOrdorSearchConditionCellIdentify];
    [self.collectionBackView addSubview:_collection];
    
    self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.22];

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataList.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZSBSearchModel *model = _dataList[indexPath.row];
    CGFloat width = [WordOrdorSearchConditionCell getCellSizeWithString:model.name].width;
    return CGSizeMake(width, 30);
}

//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//    return 3;
//}
//
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
//    return 10;
//}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WordOrdorSearchConditionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:wordOrdorSearchConditionCellIdentify forIndexPath:indexPath];
    ZSBSearchModel *model = _dataList[indexPath.row];
    cell.titleLabel.text = model.name;
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.success) {
        self.success(_dataList[indexPath.row]);
    }
    [self removeFromSuperview];
}

#pragma 显示
+ (void)showSearchPickerInView:(UIView *) view array:(NSArray *)array success:(OrdorSearchSuccessblock)success failure:(OrdorSearchFailureBlock)fail {
    if (view == nil) view = [[UIApplication  sharedApplication] delegate].window ;
    
    ZSBWordOrdorSearchConditionView *conditionView = [[NSBundle bundleWithURL:[[NSBundle bundleForClass:[self class]] URLForResource:@"NewMarketLib" withExtension:@"bundle"]] loadNibNamed:@"ZSBWordOrdorSearchConditionView" owner:self options:nil].lastObject;
    conditionView.frame = CGRectMake(0, 0,  iSEESCREEN_WIDTH , iSEESCREEN_HEIGHT);
    conditionView.dataList = array;
    conditionView.success = success;
    conditionView.fail = fail;
    [conditionView.collection reloadData];
    [view addSubview:conditionView];
    
}
- (IBAction)closeButtonAction:(UIButton *)sender {
    if (self.fail) {
        self.fail();
    }
    [self removeFromSuperview];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    if (self.fail) {
        self.fail();
    }
    [self removeFromSuperview];
}


@end
